<?php
namespace app\admin\controller\haipi;

use app\admin\library\alei\Export;
use app\common\controller\Backend;
use app\common\model\haipi\TicketToken as TicketTokenModel;
class TicketToken extends Backend{

    protected object $model;

    protected array|string $preExcludeFields = ['id'];

    protected string|array $quickSearchField = ['token'];

     public function initialize(): void
    {
        parent::initialize();
        $this->model = new TicketTokenModel();
    }


    public function export()
    {
        try {
            list($where, $alias, $limit, $order) = $this->queryBuilder();
            $res = $this->model
                ->field($this->indexField)
                ->withJoin($this->withJoinTable, $this->withJoinType)
                ->alias($alias)
                ->where($where)
                ->order($order)
                ->field('token')
                ->select()
                ->toArray();

            // 导出为文本文件
            $tokens = array_column($res, 'token'); // 提取 token
            $filePath = '/tokens.txt'; // 文件路径
            file_put_contents(public_path().$filePath, implode(PHP_EOL, $tokens)); // 写入文件
            
            $full_url = url($filePath,[],false,true)->build();
            // 完整域名
        
     
        } catch (\Throwable $th) {
            $this->error($th->getMessage());
        }
        $this->success('', compact('full_url'));
    }

}